﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

using Weazel.Toad.TextEditor;
using Weazel.Toad.TextEditor.Document;

using Weazel.Gezel.Model;
using Weazel.Gezel.Model.Errors;

using Weazel.Gezel.Parser;

#endregion

namespace Weazel.Toad.Application.Items
{
  public class GezelTextFile : TextFile
  {
    private GezelSimulation simulation;
    public GezelSimulation Simulation
    {
      get
      {
        return simulation;
      }      
    }

    protected override void CloseInternal()
    {
      // stop simulation ..
      simulation.End();
    }

    /// <summary>
    /// Parse and verify the current content of this file.
    /// </summary>
    /// <param name="parseErrors">Parse errors encoutnered</param>
    /// <param name="verificationErrors">Verification errors encountered</param>
    /// <returns>The model epressed by the current content of the file (or null if error)</returns>
    public Model ParseAndVerifyModel(
      ref List<ParseError> parseErrors, 
      ref List<VerificationError> verificationErrors,
			ref List<VerificationWarning> warnings
		)
    {
      Model model = null;

      try
      {
        // make a copy of the description
        string description = (string)Contents.Clone();

        // Parse ..
        model = ParseModel.Parse(ref parseErrors, description, DisplayName);

        if (parseErrors.Count == 0)
          model.Verify(ref verificationErrors, ref warnings);

        return model;
      }
      catch (ParseException)
      {
        // ignore certain types of exceptions (non-terminated strings etc)             
      }
      catch (Exception e)
      {
        string message =
          string.Format(
            "Internal error: {0} ({1})",
            e.Message,
            e.GetType().ToString()
          );

        System.Windows.Forms.MessageBox.Show(message);
      }

      return null;
    }

    public GezelTextFile()
    {
      this.DisplayName = "unnamed.fdl";
      this.simulation = new GezelSimulation(this);
    }

    public override string FileTypeName
    {
      get { return "Gezel"; }
    }

    public override string Extension
    {
      get 
      { 
        return "fdl"; 
      }
    }
  }
}
